import { setState } from './index'
import { apiGetOrders, apiCancelOrder, apiRepayOrder, apiGetOrderDetail } from '@/api/my-orders'

const mapOrder = ['ordersUnpaid', 'ordersPaid', 'ordersHistory']

const state = {
  ordersUnpaid: [],
  ordersUnpaidNext: false,
  ordersUnpaidLast: 0,
  ordersPaid: [],
  ordersPaidNext: false,
  ordersPaidLast: 0,
  ordersHistory: [],
  ordersHistoryNext: false,
  ordersHistoryLast: 0,
  orderDetail: {}
}

const mutations = { setState }

const actions = {
  getOrders({ rootState, commit }, type = 1) {
    const token = rootState.Client.token
    const params = { page: { pageSize: 30, lastRownum: 0 }, type, orderType: 2 }
    return new Promise((resolve, reject) => {
      apiGetOrders(token, params)
        .then(res => {
          if (res.data.data && res.data.data.orderList) {
            const result = res.data.data.orderList
            const orderObj = {}
            orderObj[mapOrder[type - 1]] = result.datas
            orderObj[`${mapOrder[type - 1]}Next`] = result.page.hasNext
            orderObj[`${mapOrder[type - 1]}Last`] = result.datas.length
              ? result.datas[result.datas.length - 1].rownum
              : 0
            commit('setState', orderObj)
            resolve()
          } else {
            reject()
          }
        })
        .catch(err => reject(err))
    })
  },

  getMoreOrders({ rootState, state, commit }, type = 1) {
    console.log(state[`${mapOrder[type - 1]}Next`])
    if (!state[`${mapOrder[type - 1]}Next`]) {
      return Promise.reject('no more')
    }
    const token = rootState.Client.token
    const params = {
      page: { pageSize: 30, lastRownum: state[`${mapOrder[type - 1]}Last`] },
      type,
      orderType: 2
    }
    return new Promise((resole, reject) => {
      apiGetOrders(token, params)
        .then(res => {
          if (res.data.data && res.data.data.orderList) {
            const result = res.data.data.orderList
            const orderObj = {}
            orderObj[mapOrder[type - 1]] = result.datas
            orderObj[`${mapOrder[type - 1]}Next`] = result.page.hasNext
            orderObj[`${mapOrder[type - 1]}Last`] = result.datas.length
              ? result.datas[result.datas.length - 1].rownum
              : 0
            commit('setState', orderObj)
            resole()
          } else {
            reject()
          }
        })
        .catch(err => reject(err))
    })
  },

  cancelOrder({ rootState, dispatch }, orderNumber) {
    const token = rootState.Client.token
    apiCancelOrder(token, orderNumber).then(res => {
      if (res.data.data.cancelOrder) {
        dispatch('getOrders', 1)
        dispatch('getOrders', 2)
        dispatch('getOrders', 3)
      }
    })
  },

  repayOrder({ rootState, dispatch }, orderNumber) {
    const token = rootState.Client.token
    return new Promise((resolve, reject) => {
      apiRepayOrder(token, orderNumber).then(res => {
        if (res.data.data && res.data.data.payTravelRouteOrder) {
          const pay = res.data.data.payTravelRouteOrder
          wx.requestPayment({
            ...pay,
            timeStamp: pay.timeStamp + '',
            package: pay.payPackage,
            success: () => {
              dispatch('getOrders', 1)
              dispatch('getOrders', 2)
              dispatch('getOrders', 3)
            }
          })
        }
      })
    })
  },

  async getOrderDetail({ rootState, commit }, orderNumber) {
    const token = rootState.Client.token
    try {
      const orderDetail = await apiGetOrderDetail(token, orderNumber)
      commit('setState', { orderDetail })
    } catch (err) {
      console.error(err)
    }
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
